#include	"../H/ugens.h"
#include        <ctype.h>
#include        <stdio.h>
#include 	<math.h>
#include        "defs.h"     /* parser */
extern        mixerr;
extern  int   NBYTES;
extern  int   sfd;
extern  double dispatch();

/* delete this dummy function when actual performing cmix computation */


/* parse: points to tree to be executed once yacc gets done */
Tree     program;            

                 /*  ug_list MUST be initialized with something...	*/
struct	ug_item	ug_head =
	{ 0, 0, "" };
struct	ug_item *ug_list = &ug_head;

#define CMAX 16     /* allow up to 16 command line args to be passed to subs */
int aargc;
char *aargv[CMAX];  /* this are declared in ugens.h */
char name[NAMESIZE];/* to store file name found by card scanner, accessed by
                       m_open in sound.c */
int interactive;     /* for m to set interactive mode, set from command line
			arg later on */
FILE *fp;

main(argc,argv)
char *argv[];
{
	char	buf[256];
	float	p[MAXDISPARGS];
	short	n_args,j;
	char    *malloc(),*bufp,*cp,*infile;

/*	fflush(stdout);
	setlinebuf(stderr); /* just to make fprintf faster.  Note that
				this may not be portable to non BSD 
				systems */
	/* copy command-line args, if any*/

	for(j=1; j<argc; j++) aargv[j-1]=argv[j];
	aargc = argc;
	

	/* Now, command-line args will be available to any subroutine
	   in aargc; and char *aargv[], (both declared extern in ugens.h)
	   parsing is exactly the same then as standard C routine.
	   Maximum of CMAX arguments allowed. */

	interactive = 1; /* default is interactive mode */
	SR = 0; /* sampling rate set by sf header or commandline */
	fprintf(stderr," ---------> Cmix/%s<----------\n",argv[0]);
	/*
	 * "Introduce" functions and stuff to program
	 */
	fp = NULL;

	if(argc >= 3) {
	while((*++argv)[0] == '-') {
		argc -= 2; /* Take away two args */
		for(cp = argv[0]+1; *cp; cp++) {
			switch(*cp) { /* Grap options */
			case 'f':
				infile = *++argv;
				fp = fopen(infile,"r+");
				if(fp == NULL) {
					printf("Can't open %s\n",infile);
					exit(-1);
					}
				fprintf(stderr,"Using file %s\n",infile);
				break;
			default:  
				printf("don't know about option %c\n",*cp);
				}
			}
	        if(argc == 1) break;
		}
	}
	ug_intro();	/* introduce standard routines */
	profile();	/* introduce user-written routines etc. */

	setbuf(stdout, NULL);	/*  Want to see stdout errors	*/

	/*
	 *    The program is read from stdin, but this can be changed
	 */

	yyparse();    /* create intermediate representation */
	
	if(!interactive) exct(program);
	closesf();
	fprintf(stderr,"--------->Goodbye<---------");
}

/*
*   This is the function called be the M-language parser.
*   It in turn calls the standard cmix dispatch function.
*/

double parse_dispatch(str,args,n)
char *str;
int n;
double args[];
{
	int i1;
	printf ("============================\n");
	printf ("%s:  ",str);
	for (i1=0;i1<n;i1++) {
		printf ("%g ",args[i1]);
	}
	printf ("\n");
	return (dispatch(str,args,n));
};

yywrap () {return 1;}
